package com.iflysse.services.impl;

import com.iflysse.dao.TeacherDao;
import com.iflysse.dao.UserDao;
import com.iflysse.pojo.bo.CommonBoResp;
import com.iflysse.pojo.bo.req.TeacherIdBoReq;
import com.iflysse.pojo.bo.req.TeacherInfoBoReq;
import com.iflysse.pojo.bo.req.UserHeadImageBoReq;
import com.iflysse.pojo.bo.resp.TeacherInfoBoRes;
import com.iflysse.pojo.po.common.CommonPoResp;
import com.iflysse.pojo.po.req.TeacherInfoPoReq;
import com.iflysse.pojo.po.req.UpdateuserEmailPoReq;
import com.iflysse.pojo.po.req.UserHeadImagePoReq;
import com.iflysse.pojo.po.resp.TeacherPoResp;
import com.iflysse.services.TeacherService;
import com.iflysse.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.sql.SQLException;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    TeacherDao teacherDao;
    @Autowired
    UserDao userDao;
    /**
     * 根据老师编号查询老师信息
     *
     * @param teacherIdBoReq 老师编号
     * @return
     */
    @Override
    public TeacherInfoBoRes getTeacherInfoByTeacherId(TeacherIdBoReq teacherIdBoReq) throws SQLException {
        //dao根据老师编号获取老师信息
        TeacherPoResp teacher= teacherDao.findTeacherByTeacherId(teacherIdBoReq.getTeacherId());
        //类型转换
        TeacherInfoBoRes teacherInfoBoRes = CommonUtil.objToObj(teacher, TeacherInfoBoRes.class);
        return teacherInfoBoRes;
    }

    /**
     * 修改老师信息
     *
     * @param teacherInfoBoReq
     * @return
     */
    @Transactional
    @Override
    public CommonBoResp updateTeacher(TeacherInfoBoReq teacherInfoBoReq) throws SQLException {
            CommonBoResp commonBoResp = null;
            TeacherInfoPoReq teacherInfoPoReq = CommonUtil.objToObj(teacherInfoBoReq, TeacherInfoPoReq.class);
            //判断老师是否修改了邮箱
            String teacherEmail = teacherDao.getTeacherEmailByTeacherId(teacherInfoBoReq.getTeacherId());
            if (!teacherEmail.equals(teacherInfoBoReq.getEmail())) {
                //老师修改了密码,同步修改账户表
                //通过老师编号获取对应的用户账户编号
                String accountId = teacherDao.getAccountIdByTeacherId(teacherInfoBoReq.getTeacherId());
                //修改账户表邮箱
                UpdateuserEmailPoReq updateuserEmailPoReq = new UpdateuserEmailPoReq();
                updateuserEmailPoReq.setAccountId(accountId);
                updateuserEmailPoReq.setEmail(teacherInfoBoReq.getEmail());
                userDao.updateEmail(updateuserEmailPoReq);


            }
            CommonPoResp commonPoResp=new CommonPoResp();
            int i = teacherDao.updateTeacher(teacherInfoPoReq);
            CommonUtil.printToPoResult(commonPoResp,i);
            commonBoResp = CommonUtil.objToObj(commonPoResp, CommonBoResp.class);
        return commonBoResp;
    }

    /**
     * 修改老师的头像
     *
     * @param userHeadImageBoReq
     * @return
     */
    @Override
    public CommonBoResp updateHeadImage4Teacher(UserHeadImageBoReq userHeadImageBoReq) throws SQLException {
        UserHeadImagePoReq userHeadImagePoReq = CommonUtil.objToObj(userHeadImageBoReq, UserHeadImagePoReq.class);
        CommonPoResp commonPoResp=new CommonPoResp();
        int i = teacherDao.updateHeadImage4Teacher(userHeadImagePoReq);
        CommonUtil.printToPoResult(commonPoResp,i);
        CommonBoResp commonBoResp = CommonUtil.objToObj(commonPoResp, CommonBoResp.class);
        return commonBoResp;
    }
}
